package com.aizuda.boot.modules.flw.flow;

import com.aizuda.bpm.engine.TaskTrigger;
import com.aizuda.bpm.engine.core.Execution;
import com.aizuda.bpm.engine.model.NodeModel;

import java.util.concurrent.CompletableFuture;
import java.util.function.Function;

//@Component
public class FlowTaskTrigger implements TaskTrigger {

    @Override
    public boolean execute(NodeModel nodeModel, Execution execution, Function<Execution, Boolean> finish) {
        // 定时触发器，避免定时任务重复执行设置临时状态 execution.setSaveAsDraft(true); 执行 finish.apply(execution); 回调完成，
        // 再去执行异步逻辑，调用 flowlongEngine.executeFinishTrigger 执行完成触发器任务
        CompletableFuture.runAsync(() -> asyncExec(execution, finish));
        System.out.println("FlowTaskTrigger = " + nodeModel.getNodeName());
        return true;
    }

    public void asyncExec(Execution execution, Function<Execution, Boolean> finish) {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
            System.out.println("进入异步调用完成触发器完成函数");
           finish.apply(execution);
        }
    }
}
